package com.thinkingcoder.tccache.core.command.list;

import static com.thinkingcoder.tccache.constant.CommandConstants.ListCmd.LPOP;

import com.thinkingcoder.tccache.core.command.Command;
import com.thinkingcoder.tccache.core.model.Reply;
import com.thinkingcoder.tccache.core.TcCache;

/**
 * LPOP command.
 */
public class LPopCmd implements Command {
    @Override
    public String name() {
        return LPOP;
    }

    @Override
    public Reply<?> exec(TcCache cache, String[] args) {
        String key = getKey(args);
        int count = 1;
        if (args.length > 6) {
            String val = getValue(args);
            count = Integer.parseInt(val);
            return Reply.array(cache.lPop(key, count));
        }
        String[] ret = cache.lPop(key, count);
        return Reply.bulkString(ret == null ? null : ret[0]);
    }
}
